<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="" prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                    <el-option v-for="dict in dict.type.points_order_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="status">
                <el-date-picker v-model="queryParams.dateRange" type="daterange" align="right" unlink-panels
                range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"
                value-format="yyyy-MM-dd" style="width: 250px;" class="filter-item" />
            </el-form-item>
            
            <el-form-item label="" prop="receiverName">
                <el-input v-model="queryParams.receiverName" placeholder="请输入收货人姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="receiverPhone">
                <el-input v-model="queryParams.receiverPhone" placeholder="请输入收货人电话" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="deliveryCode">
                <el-input v-model="queryParams.deliveryCode" placeholder="请输入快递单号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="orderList" empty-text="暂无数据">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="ID" align="center" prop="id" />
            <el-table-column label="订单编号" align="center" prop="orderNo" />
            <el-table-column label="用户ID" align="center" prop="userId" />
            <el-table-column label="商品ID" align="center" prop="goodsId" />
            <el-table-column label="商品数量" align="center" prop="goodsNum" />
            <el-table-column label="商品积分" align="center" prop="goodsPoints" />
            <el-table-column label="总消耗积分" align="center" prop="totalPoints" />
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.points_order_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            
            <el-table-column label="收货信息" align="center" prop="receiverName">
                <template slot-scope="{row}">
                    <div>
                        <span>收货人: {{ row.receiverName }}</span>
                    </div>
                    <div>
                        <span>电话: {{ row.receiverPhone }}</span>
                    </div>
                    <div>
                        <span>地址: {{ row.receiverAddress }}</span>
                    </div>
                </template>
            </el-table-column>
           
            <el-table-column label="快递名称" align="center" prop="deliveryName">
                <template slot-scope="{row}">
                    <div v-if="row.deliveryName">
                        <span>快递公司: {{ row.deliveryName }}</span>
                    </div>
                    <div v-if="row.deliveryCode">
                        <span>快递单号: {{ row.deliveryCode }}</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="收货时间" align="center" prop="receivedTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.receivedTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="取消时间" align="center" prop="cancelTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="handleDetail(scope.row)">
                        详情
                    </el-button>
                    <el-button v-if="scope.row.status === 0" size="mini" type="success" @click="handleShip(scope.row)">
                        发货
                    </el-button>
                    <el-button v-if="scope.row.status === 1" size="mini" type="success"
                        @click="handlerReceiveOrder(scope.row.id)">
                        收货
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />


        <order-detail ref="detail" :order="currentData" :dialog-visible="detailVisible"
            @close="detailVisible = false" />
        <el-dialog title="发货操作" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="快递公司" :label-width="'120px'">
                    <el-input v-model="form.deliveryName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="快递单号" :label-width="'120px'">
                    <el-input v-model="form.deliveryCode" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleShipOk()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listOrder, shipOrder, receiveOrder } from "@/api/points/order";
import OrderDetail from './Detail'
export default {
    name: "Order",
    components: { OrderDetail },
    dicts: ['points_order_status'],
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 积分订单表格数据
            orderList: [],
            // 弹出层标题
            title: "",
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderNo: null,
                status: null,
                receiverName: null,
                receiverPhone: null,
                deliveryCode: null,
                dateRange: []
            },
            detailVisible: false,
            currentData: {
                orderNo: '',
                goods: {},
                receiverName: '',
                receiverPhone: '',
                receiverAddress: '',
                deliveryName: '',
                deliveryCode: '',
                remark: '',
                totalPoints: 0,
                status: 0,
                createTime: ''
            },
            form: {
                id: '',
                deliveryCode: '',
                deliveryName: ''
            },
            dialogFormVisible:false,
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick (picker) {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                        picker.$emit('pick', [start, end])
                    }
                }, {
                    text: '最近一个月',
                    onClick (picker) {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                        picker.$emit('pick', [start, end])
                    }
                }, {
                    text: '最近三个月',
                    onClick (picker) {
                        const end = new Date()
                        const start = new Date()
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
                        picker.$emit('pick', [start, end])
                    }
                }]
            },
        };
    },
    created () {
        this.getList();
    },
    methods: {
        /** 查询积分订单列表 */
        getList () {
            this.loading = true;
            listOrder(this.queryParams).then(response => {
                this.orderList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel () {
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                id: null,
                orderNo: null,
                userId: null,
                goodsId: null,
                goodsNum: null,
                goodsPoints: null,
                totalPoints: null,
                status: null,
                receiverName: null,
                receiverPhone: null,
                receiverAddress: null,
                deliveryName: null,
                deliveryCode: null,
                deliveryTime: null,
                receivedTime: null,
                cancelTime: null,
                remark: null,
                createTime: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        handleDetail (row) {
            // this.$refs.detail.setOrder(row)
            this.currentData = row;
            this.detailVisible = true
        },
        handleShip (row) {
            this.dialogFormVisible = true;
            this.form.id = row.id;
            this.form.deliveryCode = row.deliveryCode;
            this.form.deliveryName = row.deliveryName;
        },
        handleShipOk () {
            shipOrder(this.form).then(() => {
                this.$message({
                    type: 'success',
                    message: '发货成功'
                })
                this.dialogFormVisible = false;
                this.form = {
                    id: '',
                    deliveryCode: '',
                    deliveryName: ''
                }
                this.getList()
            })
        },
        handlerReceiveOrder (id) {
            this.$confirm('确认收货吗？', "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(res => {
                receiveOrder({ id: id }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '收货成功'
                    })
                    this.getList()
                })
            })
        }

    }
};
</script>
